package cn.yangliu.web;

import cn.yangliu.comm.entity.JsonResult;
import cn.yangliu.comm.shiro.AccountService;
import cn.yangliu.entity.JpaUser;
import cn.yangliu.entity.MybatisUser;
import cn.yangliu.operation.annotation.Operate;
import cn.yangliu.poi.view.SimpleXlsView;
import cn.yangliu.poi.view.SimpleXlsxView;
import cn.yangliu.service.JpaUserService;
import cn.yangliu.shiro.AuthExceptionAdvice;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
 * 作者 杨柳
 * 创建时间 2017-08-24 15:07
 */
@RestController
public class MyController2 {

  /*  @Resource
    MybatisUserService mybatisUserService;
*/
    @Resource
    JpaUserService jpaUserService;

    @Resource
    AccountService<JpaUser> accountService;


    @RequestMapping("/insert")
    @Operate(module = "测试",action = "测试操作")
    @RequiresPermissions("asdasdasdasd")
    public JsonResult insert() throws BindException {
        MybatisUser user = new MybatisUser();
        user.setUsername("zhangfei");
        user.setPassword("guanyu");
    /*    mybatisUserService.insert(user);*/


        JpaUser jpaUser  = new JpaUser();

        jpaUser.setUsername("zhaoyun");
        jpaUser.setPassword("machao");
//        jpaUser.setId(1L);
//        jpaUserService.insert(jpaUser);
        jpaUserService.insert(jpaUser);

        JpaUser jpaUser1=accountService.findAccountByUsernameAndPassword("1","2");
        System.out.println(jpaUser1);
        return JsonResult.getInstance();
    }


    @RequestMapping("/export")
    @Operate(module = "导出",action = "测试")
    public ModelAndView export(int flag){
        System.out.println("123");
        System.out.println(1222);
        System.out.println(123123123);
        List<Item> items = Arrays.asList(new Item("打火机",new BigDecimal("12.5")),new Item("香烟",new BigDecimal("24.0")));
        Map<String,Object> dataMap = new HashMap<>();
        dataMap.put("data",items);
        LinkedHashMap<String,String> mapping =new LinkedHashMap<>();
        mapping.put("名称22222","name");
        mapping.put("价格","price");
        View view = null;
        if (flag==1){
            view = new SimpleXlsView(mapping);
        }
        if (flag==2){
            view = new SimpleXlsxView(mapping);
        }
        ModelAndView mav = new ModelAndView(view,dataMap);
        return mav;
    }
}
